package com.lucweb.action;


import com.lucweb.entity.BaseDict;
import com.lucweb.entity.TaskBuy;
import com.lucweb.entity.TaskMaterial;
import com.lucweb.service.BaseDictService;
import com.lucweb.service.BuyService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

@Controller("buyAction")
@Scope("prototype")
public class BuyAction extends ActionSupport{

    @Resource
    private BuyService buyService;

    @Resource
    private BaseDictService baseDictService;




    private TaskBuy taskBuy;

    private TaskMaterial taskMaterial;

    private int taskId;
    private int materialId;

    //新建购买窗口
    public String newBuy(){

        //设置id
        int bid = buyService.getmaxId();

        List<BaseDict> nameList=baseDictService.getListByTypeCode("3");
        List<BaseDict>companyList=baseDictService.getListByTypeCode("2");

        ActionContext.getContext().put("nameList",nameList);
        ActionContext.getContext().put("bid",bid);
        ActionContext.getContext().put("companyList",companyList);


        return "buys";

    }

    //修改购买
    public String editBuy(){

        taskBuy = buyService.gettaskbuyById(taskId);

        ActionContext.getContext().put("taskBuy", taskBuy);

        //刷新列表
        List<TaskMaterial>materialList=buyService.showMaterial(taskId);
        ActionContext.getContext().put("materialList",materialList);


        List<BaseDict>nameList=baseDictService.getListByTypeCode("3");
        List<BaseDict>companyList=baseDictService.getListByTypeCode("2");


        ActionContext.getContext().put("nameList",nameList);
        ActionContext.getContext().put("companyList",companyList);




        System.out.println(taskBuy);



        return "buys";

    }

    //删除
    public String print(){

        ActionContext.getContext().put("taskBuy", taskBuy);

        //刷新列表
        List<TaskMaterial>materialList=buyService.showMaterial(taskBuy.getId());
        ActionContext.getContext().put("materialList",materialList);


        return "printBuy";

    }

    //删除
    public String deleteBuy(){

        buyService.deletebuytask(taskId);

        //刷新页面
        List<TaskBuy>buyList=buyService.showbuy();
        ActionContext.getContext().put("buyList",buyList);

        return "addbuy";

    }

    //保存更新
    public String saveOrUpdate(){

        System.out.println(taskBuy);


        //先保存
        buyService.saveOrUpdate(taskBuy);

        //刷新页面
        List<TaskBuy>buyList=buyService.showbuy();
        ActionContext.getContext().put("buyList",buyList);

        return "addbuy";

    }

    //获取采购列表
    public String buyshow(){


        List<TaskBuy>buyList=buyService.showbuy();
        ActionContext.getContext().put("buyList",buyList);

        return "addbuy";

    }

    //获取采购列表
    public String materialshow(){

        List<BaseDict> nameList=baseDictService.getListByTypeCode("3");
        List<BaseDict>companyList=baseDictService.getListByTypeCode("2");

        ActionContext.getContext().put("nameList",nameList);
        ActionContext.getContext().put("companyList",companyList);

        List<TaskMaterial>materialList=buyService.showMaterial(taskId);
        ActionContext.getContext().put("materialList",materialList);

        return "buys";

    }


    //编辑采购明细
    public String editmaterial(){

        //1保存当前列表
        buyService.saveOrUpdate(taskBuy);
        ActionContext.getContext().put("taskBuy",taskBuy);

        taskMaterial = buyService.gettaskMaterialById(materialId);


        ActionContext.getContext().put("taskMaterial",taskMaterial);

        return "material";

    }

    //删除采购明细
    public String deletematerial(){

        List<BaseDict> nameList=baseDictService.getListByTypeCode("3");
        List<BaseDict>companyList=baseDictService.getListByTypeCode("2");
        ActionContext.getContext().put("nameList",nameList);
        ActionContext.getContext().put("companyList",companyList);

        buyService.deletematerialtask(materialId);

        //1保存当前列表
        buyService.saveOrUpdate(taskBuy);
        ActionContext.getContext().put("taskBuy",taskBuy);


        List<TaskMaterial>materialList=buyService.showMaterial(taskBuy.getId());
        ActionContext.getContext().put("materialList",materialList);

        return "buys";

    }

    //新建采购列表
    public String newmaterial(){




       //1保存当前列表
        buyService.saveOrUpdate(taskBuy);
        ActionContext.getContext().put("taskBuy",taskBuy);


        return "material";

    }

    //获取保存列表
    public String materialsaveorupdate(){

        List<BaseDict> nameList=baseDictService.getListByTypeCode("3");
        List<BaseDict>companyList=baseDictService.getListByTypeCode("2");

        ActionContext.getContext().put("nameList",nameList);
        ActionContext.getContext().put("companyList",companyList);

        //先保存采购列表
        buyService.materialsaveOrUpdate(taskMaterial);

        //取出保存buy对象
        TaskBuy taskBuy = buyService.gettaskbuyById(taskMaterial.getBuyId());
        //刷新列表
        List<TaskMaterial>materialList=buyService.showMaterial(taskMaterial.getBuyId());
        ActionContext.getContext().put("materialList",materialList);
        ActionContext.getContext().put("taskBuy",taskBuy);

        return "buys";

    }


    public TaskBuy getTaskBuy() {
        return taskBuy;
    }

    public void setTaskBuy(TaskBuy taskBuy) {
        this.taskBuy = taskBuy;
    }

    public int getTaskId() {
        return taskId;
    }

    public void setTaskId(int taskId) {
        this.taskId = taskId;
    }


    public TaskMaterial getTaskMaterial() {
        return taskMaterial;
    }

    public void setTaskMaterial(TaskMaterial taskMaterial) {
        this.taskMaterial = taskMaterial;
    }

    public int getMaterialId() {
        return materialId;
    }

    public void setMaterialId(int materialId) {
        this.materialId = materialId;
    }
}
